package io.renren.modules.lucky.service.impl;

import io.renren.common.exception.RRException;
import io.renren.common.validator.Assert;
import io.renren.modules.app.form.out.AppGoodsInfoListForm;
import io.renren.modules.app.utils.JwtUtils;
import io.renren.modules.lucky.dao.AppGoodsInfoDao;
import io.renren.modules.lucky.entity.AppGoodsInfoEntity;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.lucky.dao.AppGoodsDzscDao;
import io.renren.modules.lucky.entity.AppGoodsDzscEntity;
import io.renren.modules.lucky.service.AppGoodsDzscService;
import org.springframework.transaction.annotation.Transactional;

@Service("appGoodsDzscService")
public class AppGoodsDzscServiceImpl extends ServiceImpl<AppGoodsDzscDao, AppGoodsDzscEntity> implements AppGoodsDzscService {

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private AppGoodsInfoDao goodsInfoDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AppGoodsDzscEntity> page = this.page(
                new Query<AppGoodsDzscEntity>().getPage(params), new QueryWrapper<AppGoodsDzscEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional(readOnly = false)
    public int addGoodsdzsc(String userToken, String userId, String goodId, String dzscType) {

        String id = jwtUtils.getClaimByToken(userToken).getSubject();
        if(id.equals(userId)) {

            AppGoodsDzscEntity entity = new AppGoodsDzscEntity();
            entity.setDzscType(Integer.parseInt(dzscType));
            entity.setGoodsId(Integer.valueOf(goodId));
            entity.setUserId(Integer.valueOf(userId));
            entity.setCreateTime(new Date());
            entity.setUpdateTime(new Date());
            entity.setDzscFlag(1);

            int count = baseMapper.insert(entity);

            return  count;

        }else{
            throw new RRException("请求用户id与token不匹配");
        }
    }

    @Override
    @Transactional(readOnly = false)
    public int removeGoodsdzsc(String userToken, String userId, String goodId) {

        String id = jwtUtils.getClaimByToken(userToken).getSubject();
        if(id.equals(userId)) {

            AppGoodsDzscEntity entity = baseMapper.selectById(goodId);
            if (entity == null) {

            }
            entity.setDzscFlag(0);
            return baseMapper.updateById(entity);

        }else{
            throw new RRException("请求用户id与token不匹配");
        }
    }

    @Override
    public List<AppGoodsInfoListForm> queryPageForFavors(String userToken, String userId, Integer curPage, Integer pageSize)
            throws InvocationTargetException, IllegalAccessException {

        List<AppGoodsDzscEntity> dList =  getGoodsDzscByuser(userToken, userId);

//        List<AppGoodsInfoEntity> list = new ArrayList<>();

        if (dList.size() > 0) {
            List<Integer> idList = new ArrayList<>();
            for (AppGoodsDzscEntity d: dList) {
                idList.add(d.getId());
            }

            QueryWrapper<AppGoodsInfoEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id", idList);
            queryWrapper.orderByDesc("id");
            queryWrapper.last("limit " + (curPage > 0 ? curPage - 1 : curPage) * pageSize + ", " + (curPage == 0 ? pageSize :  curPage * pageSize));
            List<AppGoodsInfoEntity> list = goodsInfoDao.selectList(queryWrapper);

            Assert.isNull(list, "商品列表为空");

            List<AppGoodsInfoListForm> goodsInfoList = new ArrayList<>();

            for(AppGoodsInfoEntity goodsInfoEntity: list) {
                AppGoodsInfoListForm goodsInfoForm = new AppGoodsInfoListForm();
                BeanUtils.copyProperties(goodsInfoForm, goodsInfoEntity);
                goodsInfoList.add(goodsInfoForm);
            }
            return goodsInfoList;
        }

        return null;


    }

    private List<AppGoodsDzscEntity> getGoodsDzscByuser(String userToken, String userId) {

        String id = jwtUtils.getClaimByToken(userToken).getSubject();
        if(id.equals(userId)) {
            QueryWrapper<AppGoodsDzscEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userId);
            queryWrapper.eq("", 2);
            List<AppGoodsDzscEntity> list = baseMapper.selectList(queryWrapper);
            return list;
        }else{
            throw new RRException("请求用户id与token不匹配");
        }

    }

}